package com.jzqf.core.http.lambda

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

//别名
typealias Block = suspend CoroutineScope.() -> Unit
typealias Error = suspend (e: Throwable) -> Unit

fun CoroutineScope.launchUI(
    block: Block, error: Error? = null
) = launch(Dispatchers.Main) {
    try {
        block()//UI线程
    } catch (e: Throwable) {
//        error?.invoke(handleException(e))//处理异常
//        error(ExceptionHandle.handleException(e))//作用同上
    }
}

fun CoroutineScope.launchIO(
    block: Block, error: Error? = null
) = launch(Dispatchers.IO) {
    try {
        block()//IO线程
    } catch (e: Throwable) {
        error?.invoke(e)//处理异常
    }
}